<?php

class TeacherStartCourseServer {
	public static $status = array(
		'booking'=>2,
		'notBooking'=>1
	);

	
	public function weeksTimeStamps($tmpc=0,$advanceNuber=1,$date=''){
		$tmpe =  $advanceNuber*86400;
		$tmpb = 604800*$tmpc;
		if (empty($date)){
			$date = strtotime(date('Y-m-d'));
		}
		$start  = $date+$tmpe+$tmpb;
		$tmpa= array();
		$end = $start+604800;
		while ($start <$end){
			$tmpa[] = $start;
			$start= $start+86400;
		}
		return $tmpa;
	}



	public function weeksTimeStampsManager($tmpc=0,$advanceNuber=0,$date=''){
		$tmpe =  $advanceNuber*86400;
		$tmpb = 604800*$tmpc;
		if (empty($date)){
			$date = strtotime(date('Y-m-d'));
		}
		$start  = $date+$tmpe+$tmpb;
		$tmpa= array();
		$end = $start+604800+86400;
		while ($start <$end){
			$tmpa[] = $start;
			$start= $start+86400;
		}
		return $tmpa;
	}
	
	public function startCourse($date,$time,$type,$teacherId=''){
		$teacherId = $teacherId;
		if ($type==1){
			$tmp = HttpDaoManager::getTeacherStartCourseDao()->getByTeacherId($teacherId, $date, $time);
			if (empty($tmp)){
				$data = array(
					'teacher_id' =>	$teacherId,
					'date'       => $date,
					'time'       => $time
				);
				HttpDaoManager::getTeacherStartCourseDao()->add($data);
			}
		}
		else{
			HttpDaoManager::getTeacherStartCourseDao()->delete($teacherId, $date, $time);
		}
	}
	
	public function startAllCourse($time_list,$type,$teacherId=''){
		$teacherId = empty($teacherId)?$_SESSION['admin']['teacher_id']:$teacherId;
		if ($type==1){
			$date_array = explode('-',$time_list[0]);
			$date = $date_array[0];
			$book_set_list = HttpDaoManager::getTeacherStartCourseDao()->getListByTeacherId($teacherId, $date);
			$time_id_list = Common::array_value_list($book_set_list, 'time');
			$data_list = array();
			foreach($time_list as $k=>$v){
				$data = array();
				$time = explode('-', $v);
				if(in_array($time[1],$time_id_list)){
					continue;
				}
				$data['teacher_id'] = $teacherId;
				$data['date'] = $time[0];
				$data['time'] = $time[1];
				array_push($data_list,$data);
			}
			if (!empty($data_list)){
				$column_list  = array('teacher_id','date','time');
				HttpDaoManager::getTeacherStartCourseDao()->addAll($column_list,$data_list);
			}
		}
		else{
			$date_array = explode('-',$time_list[0]);
			$date = $date_array[0];
			HttpDaoManager::getTeacherStartCourseDao()->deleteAll($teacherId, $date);
		}
	}
	public function saveConfirmAllCourse($date_time_list,$type,$teacherId=''){
		$teacherId = empty($teacherId)?$_SESSION['admin']['teacher_id']:$teacherId;
		$date_array = explode('-',$date_time_list[0]);
		$date = $date_array[0];
		$data_list = array();
		foreach($date_time_list as $k=>$v){
			$time = explode('-', $v);
			$data_list[$time[0]]['date'] = $time[0];
			$data_list[$time[0]]['time'][$time[1]] = $time[1];
		}
		
		foreach($data_list as $k=>$v){
			$updata = array();
			$updata['date'] = $v['date'];
			$updata['time_list']=$v['time'];
			if(!empty($updata['time_list'])){
				$confirm_status = TEACHER_STATUS_CONFIRMED;
				$date = $updata['date'];
				$time_list = "(".implode(',', $updata['time_list']).")";
				$where = array();
				$where['teacher_id'] = $teacherId;
				$where['date'] = $date;
				$where['time_list'] = $time_list;
				HttpDaoManager::getTeacherStartCourseDao()->updateConfirmStatus($confirm_status,$where);
			}				
 		}
	}
	public function booking($id){
		HttpDaoManager::getTeacherStartCourseDao()->updateStatus(self::$status['booking'], $id);
	}
	/**
	 *获得日期上的课 
	 */
	public function getCourseTimeByData($date=array(),$teacherId=''){
		$teacherId=  empty($teacherId)?$_SESSION['admin']['teacher_id']:$teacherId;
		$bookCourse = HttpServerManager::getBookCourseServer();
		foreach ($date as $v){
			$tmp =   HttpDaoManager::getTeacherStartCourseDao()->getListByTeacherId($teacherId, $v);
			$data[$v]=array();
			foreach ($tmp as $v1){
				$data[$v][$v1['time']]['status']=$v1['status'];
				$data[$v][$v1['time']]['confirm_status']=$v1['confirm_status'];
			}
		}
		return $data;
	}
	public function getCourseTimeByDataStatus($date=array(),$teacherId,$status =1){
		foreach ($date as $v){
			$where = array();
			$where['date'] = $v;
			$where['teacher_id'] =$teacherId;
			$where['status'] = $status;
			$where['confirm_status'] = TEACHER_STATUS_CONFIRMED;
			$tmp = HttpDaoManager::getTeacherStartCourseDao()->getOpenListByTeacherId($where);
			$data[$v]=array();
			foreach ($tmp as $v1){
				$data[$v][$v1['time']]['class_time_flg'] =1;
				$data[$v][$v1['time']]['id']=$v1['id'];
				$data[$v][$v1['time']]['open_date']=$v1['date'];
				$data[$v][$v1['time']]['open_time']=$v1['time'];
				$data[$v][$v1['time']]['teacher_id']=$v1['teacher_id'];
			}
		}
		
		return $data;
	}
	public function userGet($id){
		$date = strtotime(date('Y-m-d'));
		$tmp = HttpDaoManager::getTeacherStartCourseDao()->userGet($id,$date,1);
		if (empty ($tmp)){
			$tmpa =  TEACHER_ERROR_NOE;
		}
		else{
			if ($date ==$tmp['date']){
				$minute = date('i');
				$time = '';
				if(floor(($minute+30)/60)==0){
					$minute = $minute+30;
					$time = date('G').$minute;
				}else{
					$minute = sprintf('%02d',($minute+30)%60);
					$hour =  date('G')+1;
					$time = $hour.$minute;
				}
				
				if ($time<=$tmp['time']){
					$tmpa =  $tmp; 
				}
				else{
					$tmpa = TEACHER_ERROR_NOE;
				}
			}
			else{
				$tmpa =  $tmp;
			}
		}
		return $tmpa;
	}
	/**
	 *获取 当前月迟到次数
	 */
	public function getLateNumber($teacherId){
		if (date('d')>15){
			$start  =  strtotime(date('Y-m').'-16');
			$y = date('Y');
			$m = date('m')+1;
			if ($m>12){
				$y++;
				$m = 1;
			}
			$end  =  strtotime($y.'-'.$m.'-1')-86400;
	
		}
		else{
			$start  =  strtotime(date('Y-m-').'-1');
			$end  =  strtotime(date('Y-m-').'-15');
		}
		$where = array(
				'book_date' =>array(
						array($start,'>='),
						array($end,'<='),
				),
				'teacher_id' =>$teacherId,
				'report_status' =>BOOK_REPORT_STATUS_3
		);
		return HttpDaoManager::getBookCourseDao()->searchNumber($where);
	}
}

?>